package com.example.heraclitoapp;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.example.heraclitoapp.R;
import com.example.heraclitoapp.MainActivity;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

public class Logoff extends AsyncTask<String, String, String> {
	
			private static final String NAMESPACE = "http://ws.org.br/";
			private static final String METHOD_NAME3 = "performLogoff";
			private static final String URL = "http://obaa.unisinos.br:80/WSServer/WSAgent?wsdl";
			public static final String SOAP_ACTION3 = NAMESPACE + METHOD_NAME3;
		

			SoapObject request3 = new SoapObject(NAMESPACE, METHOD_NAME3);
			Context mContext;
			
			public Logoff(Context mContext) {
				this.mContext = mContext;
			}
			
			@Override
			protected String doInBackground(String... params) {
				request3.addProperty("userId", params[0]);
				
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
						SoapEnvelope.VER11);
				envelope.setOutputSoapObject(request3);

				HttpTransportSE transport = new HttpTransportSE(URL);
				String resposta3 = new String();

				try {
					transport.call(SOAP_ACTION3, envelope);

					Object response3 = envelope.getResponse();

					if (response3 != null) {
						if (response3 instanceof SoapPrimitive) {
							SoapPrimitive respostaLogoff = (SoapPrimitive) response3;
							resposta3 = respostaLogoff.toString();
							}					// mensagemLogin.setCurrentText(resposta);
						}

				} catch (IOException e) {
					
				} catch (XmlPullParserException e) {
				
				} catch (Exception e) {
					
				}

				
				return resposta3;
			}
			
			@Override
			protected void onPostExecute(String result) {
				if (result.contains("Deslogado")) {		
					MainActivity.logado = 0;
	 				MainActivity.logadoComo.setText("");
					Toast.makeText(mContext, mContext.getString(R.string.deslogado_sucesso), Toast.LENGTH_LONG).show();
					MainActivity.btn_login.setText("Login");
	 				
				} else {
					Toast.makeText(mContext, mContext.getString(R.string.impossivel_deslogar), Toast.LENGTH_LONG).show();
				}
			}
			
		}

